﻿/*******************************************************************************
* file  : rsa key text
* creat : apleilx
* data  : 2017/03/02
*******************************************************************************/
#include "common.h"
#include "sys_task.h"
#include "os_obj.h"
#include "bsp_api.h"
#include "mbedtls/platform.h"
#include "mbedtls/bignum.h"
#include "mbedtls/rsa.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"

/*******************************************************************************
* @brief  硬件时钟获取，原timing.c中的函数，该文件仅UINX类系统支持，提出该函数
* \param[in] none
* \retval: none
*******************************************************************************/
unsigned long mbedtls_timing_hardclock( void )
{
    return bsp_cpu_clks_get();
}

/*******************************************************************************
* @brief  伪随机种子
* \param[in] none
* \retval: none
*******************************************************************************/
int rsa_random_entropy_poll( 
	void *data, 
	unsigned char *output,                     
	size_t len, 
	size_t *olen)
{
    size_t cnt = 0;
    unsigned char *p = output;
	
    ((void) data);

    *olen = 0;

    while( cnt < len)
    {
		*p++ = ((uu8*)sys_st.info.srand)[cnt & 0x07];
        cnt++;
		
		if(0 == cnt & 0x07)
		{
        	osDelay(50);
		}
    }

    *olen = len;
    return( 0 );
}

/*******************************************************************************
* @brief  显示rsa密匙
* \param[in] none
* \retval: none
*******************************************************************************/
void dump_rsa_key(mbedtls_rsa_context *ctx)
{
	// bits
	size_t dump_buff_size;
    size_t olen;
	
	dump_buff_size = mbedtls_mpi_bitlen(&ctx->private_N) >> 3;
	dump_buff_size <<= 1;
	dump_buff_size += 16;
	
    char *buf = malloc(dump_buff_size);
	
    mbedtls_printf("\n  +++++++++++++++++ rsa keypair +++++++++++++++++\n\n");
    mbedtls_mpi_write_string(&ctx->private_N , 16, buf, dump_buff_size, &olen);
    mbedtls_printf("N: %s\n", buf); 

    mbedtls_mpi_write_string(&ctx->private_E , 16, buf, dump_buff_size, &olen);
    mbedtls_printf("E: %s\n", buf);

    mbedtls_mpi_write_string(&ctx->private_D , 16, buf, dump_buff_size, &olen);
    mbedtls_printf("D: %s\n", buf);

    mbedtls_mpi_write_string(&ctx->private_P , 16, buf, dump_buff_size, &olen);
    mbedtls_printf("P: %s\n", buf);

    mbedtls_mpi_write_string(&ctx->private_Q , 16, buf, dump_buff_size, &olen);
    mbedtls_printf("Q: %s\n", buf);

    mbedtls_mpi_write_string(&ctx->private_DP, 16, buf, dump_buff_size, &olen);
    mbedtls_printf("DP: %s\n", buf);

    mbedtls_mpi_write_string(&ctx->private_DQ, 16, buf, dump_buff_size, &olen);
    mbedtls_printf("DQ: %s\n", buf);

    mbedtls_mpi_write_string(&ctx->private_QP, 16, buf, dump_buff_size, &olen);
    mbedtls_printf("QP: %s\n", buf);
    mbedtls_printf("\n  +++++++++++++++++ rsa keypair +++++++++++++++++\n\n");
	
	free(buf);
}

/*******************************************************************************
* @brief  导出公匙
* \param[in] none
* \retval: none
*******************************************************************************/
void dump_rsa_pub_key_export(mbedtls_rsa_context *ctx)
{

}

/*******************************************************************************
* @brief  导出私匙
* \param[in] none
* \retval: none
*******************************************************************************/
void dump_rsa_priv_key_export(mbedtls_rsa_context *ctx)
{
	// bits
	size_t dump_buff_size;
    size_t olen;
	
	dump_buff_size = mbedtls_mpi_bitlen(&ctx->private_N) >> 3;
	dump_buff_size <<= 1;
	dump_buff_size += 16;
	
    char *buf = malloc(dump_buff_size);
	
    mbedtls_printf("\nprivte key export =>\n\n");
	
    mbedtls_mpi_write_string(&ctx->private_N , 16, buf, dump_buff_size, &olen);

    mbedtls_mpi_write_string(&ctx->private_E , 16, buf, dump_buff_size, &olen);

    mbedtls_mpi_write_string(&ctx->private_D , 16, buf, dump_buff_size, &olen);

    mbedtls_mpi_write_string(&ctx->private_P , 16, buf, dump_buff_size, &olen);

    mbedtls_mpi_write_string(&ctx->private_Q , 16, buf, dump_buff_size, &olen);

    mbedtls_mpi_write_string(&ctx->private_DP, 16, buf, dump_buff_size, &olen);

    mbedtls_mpi_write_string(&ctx->private_DQ, 16, buf, dump_buff_size, &olen);

    mbedtls_mpi_write_string(&ctx->private_QP, 16, buf, dump_buff_size, &olen);

	free(buf);
}

/*******************************************************************************
* @brief  导入公匙
* \param[in] none
* \retval: none
*******************************************************************************/
void dump_rsa_pub_key_import(mbedtls_rsa_context *ctx)
{

}

/*******************************************************************************
* @brief  导入私匙
* \param[in] none
* \retval: none
*******************************************************************************/
void dump_rsa_priv_key_import(mbedtls_rsa_context *ctx)
{

}
